package com.sgb.kjwl.view.widget.self.permission;

import android.text.TextUtils;

import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.view.widget.self.MenuClickUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心--权限
 */
public class UserPermissionUtils {

    private static volatile UserPermissionUtils mUserPermissionUtils;

    //商品
    private ArrayList<PickFilterEntity<Object>> mGoodMenus = new ArrayList();
    //租赁
    private ArrayList<PickFilterEntity<Object>> mLeaseMenus = new ArrayList();
    //物流
    private ArrayList<PickFilterEntity<Object>> mTransMenus = new ArrayList();
    //付款订单
    private ArrayList<PickFilterEntity<Object>> mPayMenus = new ArrayList();

    //集采菜单
    private ArrayList<PickFilterEntity<Object>> mGetCentralizedMiningMenus = new ArrayList();

    private ArrayList<PickFilterEntity<Object>> mPostCentralizedMiningMenus = new ArrayList();

    //商品--我的询价
    private ArrayList<PickFilterEntity<Object>> mShopEnquiryMenus = new ArrayList();

    //审核订单
    private ArrayList<PickFilterEntity<Object>> mCheckMenus = new ArrayList();

    private ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> mMenus = new ArrayList<>();

    private ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> mCentralizedTotalMenus = new ArrayList<>();

    private UserPermissionUtils(){

    }
    public static UserPermissionUtils getInstance(){
        try {
            if (mUserPermissionUtils == null){
                synchronized (MenuClickUtils.class){
                    mUserPermissionUtils = new UserPermissionUtils();
                }
            }
        } catch (Exception e){

        }
        return mUserPermissionUtils;
    }

   //获取权限
   public void getPermission(List<PermissionMenuEntity> data, BasePermissionCallBack callBack){
       mPayMenus.clear();
       mGoodMenus.clear();
       mTransMenus.clear();
       mLeaseMenus.clear();
       mMenus.clear();
       mCheckMenus.clear();
       mGetCentralizedMiningMenus.clear();
       mPostCentralizedMiningMenus.clear();
       mCentralizedTotalMenus.clear();
       mShopEnquiryMenus.clear();
       if (!data.isEmpty()) {
           PickFilterEntity<Object> mSecondEntity;
           PickFilterEntity<ArrayList<PickFilterEntity<Object>>> mEntity;
           for (PermissionMenuEntity entity : data) {
               if (entity != null) {
                    if ("oneLevel_xiaoshoujingying".equals(entity.getName())) {
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_caigouguanli".equals(entity1.getName())){
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()) {
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List) {
                                           if (("3_caigoudingdan").equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("采购订单");
                                               mSecondEntity.setNo("5");
                                               mGoodMenus.add(mSecondEntity);
                                           } else if ("2_wodexunjia".equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mShopEnquiryMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               } else if ("twoLevel_xiaoshouguanli".equals(entity1.getName())){
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()) {
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List) {
                                           if (("3_xiaoshoudingdan").equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("销售订单");
                                               mSecondEntity.setNo("6");
                                               mGoodMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   } else if ("oneLevel_zulinjingying".equals(entity.getName())) {
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_zulinguanli".equals(entity1.getName())){
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()) {
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List) {
                                           if (("4_chuzudingdan").equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("出租订单");
                                               mSecondEntity.setNo("7");
                                               mLeaseMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               } else if ("twoLevel_qiuzuguanli".equals(entity1.getName())){
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()) {
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List) {
                                           if (("4_qiuzudingdan").equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("租入订单");
                                               mSecondEntity.setNo("8");
                                               mLeaseMenus.add(0, mSecondEntity);
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   } else if ("oneLevel_wuliuhuoyun".equals(entity.getName())) {
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_huoyunguanli".equals(entity1.getName())){
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()){
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List){
                                           if (("10_yunshudingdan").equals(entity2.getName()) || ("20_yunshudingdan").equals(entity2.getName())
                                                   || "19_huochedingdan".equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("物流订单");
                                               mTransMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               }

                           }
                       }
                   } else if ("oneLevel_caiwuguanli".equals(entity.getName())) {
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_duizhangzhongxin".equals(entity1.getName())){
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()){
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List){
                                           if (("8_daifudingdan").equals(entity2.getName())) {
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("待付订单");
                                               mPayMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               }

                           }
                       }
                   } else if ("oneLevel_huiyuanjingying".equals(entity.getName())) {//会员经营
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_huiyuanshouli".equals(entity1.getName())){//会员受理
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()){
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List){
                                           if (("14_pingjishenhe").equals(entity2.getName())) {//会员审核
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("客户审核");
                                               mSecondEntity.setNo("10");
                                               mCheckMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               }

                           }
                       }
                   } else if ("oneLevel_qiyeshezhi".equals(entity.getName())) {//会员经营
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_qiyeshezhi".equals(entity1.getName())){//会员受理
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()){
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List){
                                           if (("18_yuangongshenhe").equals(entity2.getName())) {//会员审核
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mSecondEntity.setName("员工审核");
                                               mSecondEntity.setNo("9");
                                               mCheckMenus.add(0, mSecondEntity);
                                           }
                                       }
                                   }
                               }

                           }
                       }
                   } else if ("oneLevel_jicaiguanli".equals(entity.getName())) {//集采管理
                       if (entity.getList() != null && !entity.getList().isEmpty()) {
                           List<PermissionMenuEntity.ListEntity> entityList = entity.getList();
                           for (PermissionMenuEntity.ListEntity entity1 : entityList) {
                               if ("twoLevel_jicaiguanli".equals(entity1.getName())){//集采管理
                                   List<PermissionMenuEntity.ListEntity> entity1List = entity1.getList();
                                   if (entity1List != null && !entity1List.isEmpty()){
                                       for (PermissionMenuEntity.ListEntity entity2 : entity1List){
                                           if (("8_wodejicai").equals(entity2.getName())) {//需方发起 供方发起 企业专项 三个菜单要么同时存在 要么都没有
                                               mSecondEntity = new PickFilterEntity<Object>();
                                               mGetCentralizedMiningMenus.add(mSecondEntity);
                                           }
                                       }
                                   }
                               }

                           }
                       }
                   }
               }
           }
           if (!mGoodMenus.isEmpty()) {
               mEntity = new PickFilterEntity<>();
               mEntity.setNo("1");
               mEntity.setName("商品订单");
               mEntity.setChildren(mGoodMenus);
               mMenus.add(mEntity);
           }
           if (!mLeaseMenus.isEmpty()) {
               mEntity = new PickFilterEntity<>();
               mEntity.setNo("2");
               mEntity.setName("租赁订单");
               mEntity.setChildren(mLeaseMenus);
               mMenus.add(mEntity);
           }
           if (!mTransMenus.isEmpty()) {
               mEntity = new PickFilterEntity<>();
               mEntity.setNo("3");
               mEntity.setName("物流运输");
               mMenus.add(mEntity);
           }
           if (!mGetCentralizedMiningMenus.isEmpty()){
               mGetCentralizedMiningMenus.clear();
               mGetCentralizedMiningMenus.add(new PickFilterEntity<Object>("12", "加盟单位"));
               mGetCentralizedMiningMenus.add(new PickFilterEntity<Object>("13", "报价单位"));
               mEntity = new PickFilterEntity<>();
               mEntity.setNo("14");
               mEntity.setName("收到的集采");
               mEntity.setChildren(mGetCentralizedMiningMenus);
               mCentralizedTotalMenus.add(mEntity);
               mPostCentralizedMiningMenus.add(new PickFilterEntity<Object>("16", "需方发起"));
               mPostCentralizedMiningMenus.add(new PickFilterEntity<Object>("17", "企业专项"));
               mPostCentralizedMiningMenus.add(new PickFilterEntity<Object>("18", "供方发起"));
               mEntity = new PickFilterEntity<>();
               mEntity.setNo("15");
               mEntity.setName("发起的集采");
               mEntity.setChildren(mPostCentralizedMiningMenus);
               mCentralizedTotalMenus.add(mEntity);
           }
       }
       callBack.getOrderPermission(mMenus);
       callBack.getPayPermission(mPayMenus);
       callBack.getCheckPermission(mCheckMenus);
       callBack.getCentralizedMiningPermission(mCentralizedTotalMenus);
       callBack.getShopEnquiryPermission(mShopEnquiryMenus);
   }

   //获取会员具体权限
   public boolean getMemberPermission(List<PermissionMenuEntity> data, String... permission){
        if (data == null || data.isEmpty()) return false;
        if (permission.length != 2) {
            return false;
        }
       for (PermissionMenuEntity entity : data) {
           if ("oneLevel_huiyuanjingying".equals(entity.getName())) {//会员经营
               for (PermissionMenuEntity.ListEntity entity1 : entity.getList()) {
                   if (TextUtils.equals(entity1.getName(), permission[0])) {
                       if (entity1.getList() != null && !entity1.getList().isEmpty()) {
                           for (PermissionMenuEntity.ListEntity entity2 : entity1.getList()) {
                               if (entity2.getName().equals(permission[1])) {
                                   return true;
                               }
                           }
                       }
                   }
               }
           }
       }
       return false;
   }

}
